#! /usr/bin/env python
# -*- coding: utf-8 -*-
import web
import json
from web import form

import config

from application.utilities.ctGet import ZF
from application.utilities.ctAnalytics import ctAnalytics
from application.utilities.render_template import render_template

from application.models.ctModel import ctModel


class signup:
    def GET(self):
        return render_template("group/signup.html",WEB_TITLE = config.WEB_TITLE+u" 团队注册")

    def POST(self):

        if not 0:
            return render_template("group/signup.html")
        else:
            raise web.seeother('/group')

all_client = {}
class index:
	def GET(self):
		main_class = ZF()
		viewstate,is_verify,time_md5 = main_class.pre_login()
		all_client[time_md5] = (main_class, viewstate,is_verify)
		return render_template("group/login.html",is_verify=is_verify,time_md5=time_md5)

	def POST(self):
		content = web.input()
		self.xh = content['xh']
		self.pw = content['pw']	
		time_md5 = content['time_md5']
		try:
			value = all_client.pop(time_md5)
			main_class = value[0]
			viewstate = value[1]
			is_verify = value[2]
		except KeyError:
			return render.error(err_str="错误")
		
		if is_verify == 1:
			verify_code = content['verify']
		else:
			verify_code = None
		main_class.set_user_info(self.xh, self.pw)
		ret = main_class.login(viewstate,is_verify,verify_code)

		if ret != "ok":
			return render_template("error.html",err_str=ret)
		#获取html
		ct_html = main_class.get_ctable()
		#分析html
		ct_result = ctAnalytics().do(ct_html)
		#存储分析结果
		ctModel().new_ct_data(1,ct_result["ct_data"])
		
		ct_result = str(ct_result)
		if ct_result:
			error = None
			return render_template("group/index.html",table=ct_result, error=error)
		else:
			ct_result = None
			error = "can not find your index table"
			return render_template("group/index.html",table=ct_result, error=error)